#pragma once
#include "data.hpp"
#include "util.hpp"
#include "config.hpp"
#include <string>
namespace cloud
{
    class HotManager
    {
    private:
        int hot_time_;
        std::string packfile_suffix_; // 压缩文件后缀
        std::string back_dir_;        // 备份文件目录,所有上传的文件都放在这里
        std::string pack_dir_;        // 压缩文件路径
    public:
        HotManager() // 初始化成员
        {
            Config *cfg = Config::GetInstance();
            back_dir_ = cfg->GetBackfile();
            pack_dir_ = cfg->GetPackfile();
            packfile_suffix_ = cfg->GetPacksuffix();
            hot_time_ = cfg->GetHotime();
            Fileutil tmp1(back_dir_);
            Fileutil tmp2(pack_dir_); // 创建./backdir和./packdir两个目录
            if (tmp1.Exits() == false)
                tmp1.creat_director();
            if (tmp2.Exits() == false)
                tmp2.creat_director();
        }
        bool HotJudge(const std::string &filename)
        {
            Fileutil fu(filename);
            int atime = fu.fileAcctime();
            int cur_time = time(NULL);
      
            if ((cur_time - atime) > hot_time_)
                return false;
            return true;
        }
        bool RunModel()
        {
            while (1)
            {
                Fileutil fu(back_dir_);
                std::vector<std::string> vs;
                fu.ScanDirectory(&vs);
                for (auto &e : vs)
                {
                    if (HotJudge(e)) //是热点文件,不作处理
                    {
                        continue;
                    }
                    // 压缩
                    Fileutil tmp(e);
                    // 获取压缩文件名
                    BackupInfo info;
                    DataManager *data = DataManager::GetInstance();
                    if (data->GetOnebyrealpath(e, &info) == false)
                    {
                        info.NewBackupInfo(e);//为e文件获取一份备份信息
                    }
                    tmp.Compress(info.pack_path_);
                    // 删除原文件
                    tmp.Remove();
                    info.flag_ = true;
                    data->Update(info);
                }
                usleep(1000000);
            }
            return true;
        }
    };
};
